local skel = fk.CreateSkill{
  name = "sj3__huanri",
}

Fk:loadTranslationTable{
  ["sj3__huanri"] = "换日",
  [":sj3__huanri"] = "<b>锁定技</b>，你拥有“<a href=':zhuri'><i>逐日 </i></a>”；没赢的效果更改为：“用赢的牌作为判定牌进行一次<a href=':floating_thunder'>【浮雷】</a>判定”。",

  [":zhuri"] = "你的阶段结束时，若你本阶段手牌数变化过，你可以拼点：若你赢，你可以使用一张拼点牌；若你没赢，你失去1点体力或本技能直到回合结束。",

  ["#sj3__huanri-choose"] = "换日：你可以拼点，若赢，你可以使用一张拼点牌；若没赢，你用赢的牌作为判定牌进行一次【浮雷】判定",
  ["#zhuri-use"] = "逐日：你可以使用其中一张牌",

  [":floating_thunder"] = "延时锦囊牌<br/>"..
  "<b>时机</b>：出牌阶段<br/>"..
  "<b>目标</b>：你<br/>"..
  "<b>效果</b>：将【浮雷】置于目标角色的判定区里。若判定结果为♠，则目标角色受到X点雷电伤害（X为此牌判定结果为♠的次数）。判定完成后，"..
  "将此牌移动到下家的判定区里。",

  ["$sj3__huanri1"] = "",
  ["$sj3__huanri2"] = "",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and
      player.phase >= Player.Start and player.phase <= Player.Finish and not player:isKongcheng() then
      return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.to == player and move.toArea == Card.PlayerHand then
            return true
          end
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end, Player.HistoryPhase) > 0 and
      table.find(player.room.alive_players, function(p)
        return player:canPindian(p)
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return player:canPindian(p)
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = skel.name,
      prompt = "#sj3__huanri-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local pindian = player:pindian({to}, skel.name)
    if player.dead then return end
    if pindian.results[to].winner == player then
      local ids = {}
      for _, card in ipairs({pindian.fromCard, pindian.results[to].toCard}) do
        if room:getCardArea(card) == Card.DiscardPile then
          table.insertIfNeed(ids, card:getEffectiveId())
        end
      end
      ids = table.filter(ids, function (id)
        local card = Fk:getCardById(id)
        return not player:prohibitUse(card) and player:canUse(card, { bypass_times = true })
      end)
      if #ids == 0 then return false end
      room:askToUseRealCard(player, {
        pattern = ids,
        skill_name = skel.name,
        prompt = "#zhuri-use",
        extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = ids,
        }
      })
    else
      local judge = {
        who = player,
        reason = "floating_thunder",
        pattern = ".|.|spade",
      }
      if pindian.results[to].winner and table.contains({Card.Processing,Card.DiscardPile},room:getCardArea(pindian.results[to].toCard)) then
        judge.card = pindian.results[to].toCard
      else
        return
      end
      room:judge(judge)
      if judge:matchPattern() then
        room:damage{
          to = player,
          damage = 1,
          damageType = Fk:getDamageNature(fk.ThunderDamage) and fk.ThunderDamage or fk.NormalDamage,
          skillName = skel.name,
        }
      end
    end
  end,
})

return skel
